<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="关键字" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入物品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="分类" prop="categoryId">
        <treeselect style="width: 300px" v-model="queryParams.categoryId" :options="deptOptions" :show-count="true" placeholder="请选择分类" />
      </el-form-item>

      <el-form-item label="仓库" prop="storeId">
        <el-select v-model="queryParams.storeId" clearable placeholder="请选择仓库">
          <el-option
            v-for="dept in stores"
            :key="dept.id"
            :label="dept.storeName"
            :value="dept.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
          v-hasPermi="['team:goodsstore:add']"
        >添加物品</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-info"
          size="mini"
          @click="openStore()"
        >仓库管理</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-info"
          size="mini"
          @click="openClassify()"
        >分类管理</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsstoreList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="物品图片" align="center" prop="goodsCover">
        <template v-slot="scope">
          <img :src="scope.row.goodsCover" width="80px"  alt=""/>
        </template>
      </el-table-column>
      <el-table-column label="物品名称" align="center" prop="goodsName" />
      <el-table-column label="品牌" align="center" prop="brandName" />
      <el-table-column label="分类" align="center" prop="busiPath" />
      <el-table-column label="所在仓库" align="center" prop="storeName" />
      <el-table-column label="库存剩余数量" align="center" prop="amounts" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['team:goodsstore:edit']"
          >编辑</el-button>
          <el-button
            v-if="scope.row.amounts === 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['team:goodsstore:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleOpen(scope.row)"
            v-hasPermi="['team:goodsstore:info']"
          >出入库明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物品库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加出入库明细对话框 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="70%" :before-close="cancel" append-to-body>
      <!-- 表格部分 -->
      <el-table :data="outPutTableData" border>
        <el-table-column label="序号"    align="center" prop="id" />

        <el-table-column label="类型" align="center" prop="type">
          <template slot-scope="scope">
             <dict-tag :options="dict.type.goods_stock_type" :value="scope.row.type"/>
          </template>
        </el-table-column>

        <el-table-column label="操作时间" align="center" prop="inOutBusiDate" />
        <el-table-column label="物品"    align="center" prop="goodsName" />
        <el-table-column label="品牌"    align="center" prop="brandName" />
        <el-table-column label="仓库"    align="center" prop="storeName" />
        <el-table-column label="变化数量" align="center" prop="inOutAmounts" />
        <el-table-column label="单价"    align="center" prop="price" />
      </el-table >
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listStoreGoods, listGoodsstore, getGoodsstore, delGoodsstore, addGoodsstore, updateGoodsstore} from "../../../api/team/goodsstore";
import {storeAll} from "../../../api/team/store";
import {treeselect} from "../../../api/team/category";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Goodsstore",
  dicts: ["goods_stock_type"],
  components: { Treeselect },
  data() {
    return {
      showInfoForm: {},
      // 物品分类树选项
      deptOptions: undefined,
      // 出入库数据
      outPutTableData: [],
      // 出入库详情弹框
      openInfo: false,
      // 分类集合
      classifies:[],
      // 仓库集合
      stores: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物品库存表格数据
      goodsstoreList: [

      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: '',
        storeId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();

    storeAll().then((data)=> {
      this.stores = data.data
    })
    this.getTreeselect();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询物品库存列表 */
    getList() {
      this.loading = true;
      listGoodsstore(this.queryParams).then(response => {
        this.goodsstoreList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false
      this.reset();
      this.outPutTableData = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        storeId: null,
        amounts: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物品库存";
    },
    openStore(){
      this.$router.push("/store/store")
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoodsstore(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物品库存";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoodsstore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoodsstore(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物品库存编号为"' + ids + '"的数据项？').then(function() {
        return delGoodsstore(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleOpen(row){
      let details = {...row};
      this.title='出入库明细'
      this.outPutTableData.push(details);
      this.openInfo = true
    },
    openClassify(){
      this.$router.push("/goods/category")
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('team/goodsstore/export', {
        ...this.queryParams
      }, `goodsstore_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
